package com.zhaofei.webtong;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by user on 2016/4/20.
 */
public class ThreadManager {

    private ScheduledExecutorService scheduledExecutorService = null;
    private static ThreadManager threadManager = null;

    private ThreadManager() {
        scheduledExecutorService = Executors.newScheduledThreadPool(50);
    }

    public static ThreadManager getThreadManagerInstance() {
        if (threadManager == null)
            threadManager = new ThreadManager();
        return threadManager;
    }

    public void schedule(Runnable command, long delay, TimeUnit unit) {
        try {
            if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
                scheduledExecutorService.schedule(command, delay, unit);
            }
        } catch (Exception e) {

        }
    }

    public void execute(Runnable command) {
        try {
            if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
                scheduledExecutorService.execute(command);
            }
        } catch (Exception e) {
        }
    }

    public void submit(Runnable command) {
        try {
            if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
                scheduledExecutorService.submit(command);
            }
        } catch (Exception e) {

        }
    }

}
